package projetjava;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;

import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.FlickrException;
import com.aetrion.flickr.REST;
import com.aetrion.flickr.RequestContext;
import com.aetrion.flickr.auth.Auth;
import com.aetrion.flickr.auth.AuthInterface;
import com.aetrion.flickr.auth.Permission;
import com.aetrion.flickr.uploader.*;
import com.aetrion.flickr.util.IOUtilities;

/**
 * Demonstrates the authentication-process.<p>
 * 
 * If you registered API keys, you find them with the shared secret at your
 * <a href="http://www.flickr.com/services/api/registered_keys.gne">list of API keys</a>
 * 
 * @author mago
 * @version $Id: AuthExample.java,v 1.4 2007/12/01 00:05:34 x-mago Exp $
 */
public class MonFlickr {
    BufferedImage img;
    Flickr f;
    RequestContext requestContext;
    String frob = "";
    String token = "";
    Properties properties = null;

    public MonFlickr(BufferedImage img) throws ParserConfigurationException, IOException, SAXException {
        this.img=img;
        f = new Flickr("c707d4c52517c5d9841deb2d29539807", new REST());
        Flickr.debugStream = false;
        // Set the shared secret which is used for any calls which require signing.
        requestContext = RequestContext.getRequestContext();
        requestContext.setSharedSecret("5226e9da36303677");
        AuthInterface authInterface = f.getAuthInterface();
        try {
            frob = authInterface.getFrob();
        } catch (FlickrException e) {
            e.printStackTrace();
        }
        System.out.println("frob: " + frob);
        URL url = authInterface.buildAuthenticationUrl(Permission.WRITE, frob);
        System.out.println("Press return after you granted access at this URL:");
        System.out.println(url.toExternalForm());
        BufferedReader infile =
          new BufferedReader ( new InputStreamReader (System.in) );
        String line = infile.readLine();
        try {
            Auth auth = authInterface.getToken(frob);
            UploadMetaData umd = new UploadMetaData();
            umd.setAsync(false);
            umd.setDescription("toto");
            Uploader upl = new Uploader("c707d4c52517c5d9841deb2d29539807",f.getTransport());
            FileInputStream fis = new FileInputStream("C:/Documents and Settings/colle0531/Bureau/toto.jpg");
            RequestContext.getRequestContext().setAuth(auth);
            
            upl.upload(fis, umd);
        } catch (FlickrException e) {
            System.out.println("Authentication failed");
            e.printStackTrace();
        }
        
        
 
    }

}
